package com.magnificent.atlantis.client.ui.planner.panels.message;

import com.magnificent.atlantis.client.MainLoad;
import com.magnificent.atlantis.client.data.message.messages.ClientTextMessage;
import com.magnificent.atlantis.client.data.message.messages.ClientMessage;
import com.magnificent.atlantis.client.ui.planner.panels.message.content.AbstractMessageContentPanel;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Babanin
 * Date: 04.05.2009
 * Time: 14:33:46
 */
public class MessageContentPanelContainer extends JPanel {
    private static Map<Class, AbstractMessageContentPanel> messageContentPanelMap =
            new HashMap<Class, AbstractMessageContentPanel>();

    private MessageHeaderPanel headerPanel;
    private AbstractMessageContentPanel currentContentPanel;

    public MessageContentPanelContainer() {
        super(new BorderLayout());

        headerPanel = new MessageHeaderPanel(MainLoad.getPlannerData());
        add(headerPanel, BorderLayout.NORTH);
    }

    public void messageSelected(ClientMessage textMessage) {
        if (currentContentPanel != null) {
            remove(currentContentPanel);
        }

        if (textMessage != null) {
            currentContentPanel = messageContentPanelMap.get(textMessage.getClass());

            headerPanel.messageSelected(textMessage);
            currentContentPanel.messageSelected(textMessage);

            add(currentContentPanel, BorderLayout.CENTER);
        }  else {
            headerPanel.clear();
        }

        repaint();
        revalidate();
    }

    public static void register(Class messageClass, AbstractMessageContentPanel panel) {
        messageContentPanelMap.put(messageClass, panel);
    }
}
